﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlexminAPI;
using System.ServiceProcess;
using System.Diagnostics;
using System.Threading;

namespace ServiceBrowser
{
	public partial class ServiceForm : Form
	{
		private ServiceSlave slave;

		public ServiceForm(ISlave slave)
		{
			this.slave = slave as ServiceSlave;
			InitializeComponent();
			LoadServicesList();
		}

		private void LoadServicesList()
		{
			servList.Items.Clear();
			ServiceController[] services = slave.GetServices();
			int i = 0;
			foreach (ServiceController service in services)
			{
				servList.Items.Add(service.DisplayName);
				servList.Items[i].SubItems.Add(service.Status.ToString());
				servList.Items[i].SubItems.Add(service.ServiceType.ToString());
				servList.Items[i].SubItems.Add("");
				servList.Items[i].Tag = service;
				i++;
			}
		}		

		private void ShowButtons(bool show)
		{
			startBtn.Enabled = stopBtn.Enabled = pauseBtn.Enabled = restartBtn.Enabled = show;
		}

		private void ShowOnlyStartButton(bool show)
		{
			startBtn.Enabled = show;
			stopBtn.Enabled = pauseBtn.Enabled = restartBtn.Enabled = !show;
		}	

		private void servList_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (servList.SelectedItems.Count != 1)
			{
				ShowButtons(false);
				return;
			}

			switch (servList.SelectedItems[0].SubItems[1].Text)
			{
				case "Running":
					ShowOnlyStartButton(false);
					break;					
				default:
					ShowOnlyStartButton(true);
					break;
			}
		}

		private void startBtn_Click(object sender, EventArgs e)
		{
			if (servList.SelectedItems.Count != 1)
			{
				ShowButtons(false);
				return;
			}

			ServiceController service = servList.SelectedItems[0].Tag as ServiceController;
			if (service == null)
				return;

			service.Start();

			LoadServicesList();
		}

		private void pauseBtn_Click(object sender, EventArgs e)
		{
			if (servList.SelectedItems.Count != 1)
			{
				ShowButtons(false);
				return;
			}

			ServiceController service = servList.SelectedItems[0].Tag as ServiceController;
			if (service == null)
				return;

			service.Pause();

			LoadServicesList();
		}

		private void stopBtn_Click(object sender, EventArgs e)
		{
			if (servList.SelectedItems.Count != 1)
			{
				ShowButtons(false);
				return;
			}

			ServiceController service = servList.SelectedItems[0].Tag as ServiceController;
			if (service == null)
				return;

			service.Stop();

			LoadServicesList();
		}

		private void restartBtn_Click(object sender, EventArgs e)
		{
			if (servList.SelectedItems.Count != 1)
			{
				ShowButtons(false);
				return;
			}

			ServiceController service = servList.SelectedItems[0].Tag as ServiceController;
			if (service == null)
				return;

			service.Stop();
			Thread.Sleep(1200);
			service.Start();

			LoadServicesList();
		}

		private void refreshServBtn_Click(object sender, EventArgs e)
		{
			LoadServicesList();
		}
	}
}
